﻿using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using MiniFox.Configurations;
using MiniFox.Platform.Configurations;
using System.Linq.Expressions;
using System.Reflection;

namespace MiniFox.Platform.Web
{
    /// <summary>
    /// 
    /// </summary>
    public static class VerifyExtensions
    {
        static VerifyConfiguration config;
        static VerifyExtensions()
        {
            config = ConfigurationFactory.GetConfig<VerifyConfiguration>();
        }
        #region validcode
        /// <summary>
        /// 
        /// </summary>
        /// <param name="html"></param>
        /// <param name="name"></param>
        /// <param name="randcodeUrl"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static IHtmlContent ValidCode(this IHtmlHelper<dynamic> html, string name, string randcodeUrl, object htmlAttributes)
        {
            IDictionary<string, object> htmlAttrs;
            if (htmlAttributes == null)
            {
                htmlAttrs = new Dictionary<string, object>();
            }
            else
            {
                htmlAttrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            }

            string id = "img_" + name;

            if (htmlAttrs.ContainsKey("id"))
            {
                htmlAttrs["id"] = id;
            }
            else
            {
                htmlAttrs.Add("id", id);
            }

            if (string.IsNullOrEmpty(randcodeUrl))
            {
                if (config != null)
                {
                    randcodeUrl = config.RandCodeUrl;
                }
            }
            if (htmlAttrs.ContainsKey("base-url"))
            {
                htmlAttrs["base-url"] = randcodeUrl;
            }
            else
            {
                htmlAttrs.Add("base-url", randcodeUrl);
            }
            string strhtml = "<img ";
            foreach (var kv in htmlAttrs)
            {
                strhtml += $"{kv.Key}=\"{kv.Value}\" ";
            }
            strhtml += " />";

            return new HtmlString(strhtml);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="html"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static IHtmlContent ValidCodeFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            return ValidCodeFor(html, expression, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="html"></param>
        /// <param name="expression"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static IHtmlContent ValidCodeFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
        {
            MemberExpression exp = expression.Body as MemberExpression;
            ParameterExpression par = expression.Parameters[0];
            MemberInfo member = exp.Member;
            string htmlstr = string.Empty;
            if (member != null && member.MemberType != MemberTypes.Property)
                return new HtmlString(htmlstr);

            PropertyInfo property = member as PropertyInfo;
            if (property == null)
                return new HtmlString(htmlstr);

            IDictionary<string, object> htmlAttrs;
            if (htmlAttributes == null)
            {
                htmlAttrs = new Dictionary<string, object>();
            }
            else
            {
                htmlAttrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            }

            string id = "img_" + html.IdFor(expression);

            if (htmlAttrs.ContainsKey("id"))
            {
                htmlAttrs["id"] = id;
            }
            else
            {
                htmlAttrs.Add("id", id);
            }

            string randcodeUrl;
            if (config == null)
            {
                randcodeUrl = "/Validation/RandCode";
            }
            else
            {
                randcodeUrl = config.RandCodeUrl;
            }
            if (htmlAttrs.ContainsKey("base-url"))
            {
                htmlAttrs["base-url"] = randcodeUrl;
            }
            else
            {
                htmlAttrs.Add("base-url", randcodeUrl);
            }
            string strhtml = "<img ";
            foreach (var kv in htmlAttrs)
            {
                strhtml += $"{kv.Key}=\"{kv.Value}\" ";
            }
            strhtml += " />";

            return new HtmlString(strhtml);
        }
        #endregion

    }
}
